package com.hsylient.javase.chapter10;

import cn.hutool.core.util.NumberUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Slf4j
public class BigDecimalTest {

    @Test
    public void testBigDecimal() {
        BigDecimal decimal = new BigDecimal("11.123450000000000000");
        log.info("[BigDecimal测试] 原始的小数的显示风格 {}", decimal);
        log.info("[BigDecimal测试] 删掉小数后面的零 {}", decimal.stripTrailingZeros());
        log.info("[BigDecimal测试] 小数转字符串 {}", decimal.toPlainString());

        log.info("[BigDecimal测试] 除法保留8位小数向下取整 {}", BigDecimal.TEN.divide(BigDecimal.valueOf(3), 8, RoundingMode.DOWN));
        log.info("[BigDecimal测试] 除法保留8位小数向上取整 {}", BigDecimal.TEN.divide(BigDecimal.valueOf(3), 8, RoundingMode.UP));
        log.info("[BigDecimal测试] 除法保留8位小数向上取整 {}", BigDecimal.TEN.divide(BigDecimal.valueOf(3), 8, RoundingMode.UP));
    }

    @Test
    public void HuTool() {
        BigDecimal result = NumberUtil.add(BigDecimal.TEN, BigDecimal.ONE);
        log.info("[HuTool BigDecimal测试] 10 + 1 = {}", result);
    }


    public void formatBigDecimal(BigDecimal from) {
//        BigDecimal bigDecimal = new BigDecimal("1234567.8900");

        String pattern = "#,##0.########";
        String formatted = NumberUtil.decimalFormat(pattern, from);

        // 输出结果
        System.out.println(from.toEngineeringString() + "格式化后的字符串: " + formatted);
    }

    @Test
    public void testFormat() {
        formatBigDecimal(new BigDecimal("0.00000100"));
        formatBigDecimal(new BigDecimal("7.8900"));
        formatBigDecimal(new BigDecimal("67.8900"));
        formatBigDecimal(new BigDecimal("567.8900"));
        formatBigDecimal(new BigDecimal("4567.8900"));
        formatBigDecimal(new BigDecimal("1234567.8900"));
    }
}
